import {ElMessageBox, ElNotification} from 'element-plus'
import service from "../../utils/request";
import useUserStore from '@/store/modules/user'  
 
const user = useUserStore().GetInfo() 

export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('Please input the password again'));
      } else if (value !== this.form.password) {
        callback(new Error("Two inputs don't match!"))
      } else {
        callback();
      }
    };
    return {
      user: user,
      form: {
        password: '', checkPassword: '', id: '',
      },
      activeName: 'updatePwd',
      rules: {
        password: [
          { required: true, message: 'Password cannot be empty', trigger: 'blur' },
          { min: 8, max: 16, message: 'Length ranging from 8 to 16 characters', trigger: 'blur' }
        ],
          newPassword: [
            { required: true, message: 'Password cannot be empty', trigger: 'blur' },
            { min: 8, max: 16, message: 'Length ranging from 8 to 16 characters', trigger: 'blur' }
          ],
          checkPassword: [{ required: true, validator: validatePass2, trigger: 'blur' }],
      }
    }
  },

  computed: {

  },
  methods: {
    handleClick(tab, event){
      this.$router.push({ path: '/account/'+ tab.paneName })
    },
    updatePwd() {
      this.$refs['form'].validate(async (valid) => {
        if (!valid) {
          // ElNotification.warning({ title: 'Prompt information', message: 'Verification failed!', position: 'bottom-right' })
          return
        }
        ElMessageBox.confirm('After modification, you need to log in again and confirm the password change？', 'Confirmation Information', {type: 'warning'}).then(async ()=> {
          this.form.id=this.user.id
		     await service.put(`shipping/sysUser/updatePwd`, this.form)
          ElNotification.success({ title: 'Prompt information', message: 'Password changed successfully, will now be redirected to the login page！', position: 'bottom-right' })
          setTimeout(() => {
            this.$store.dispatch('LogOut')
              .then(() => { location.reload() })
          }, 3000)
        })

      })
    }
  },
  mounted() {
    //this.user = this.$store.getters.user
  },
}
